package com.wu.rabbitmq.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.wu.rabbitmq.utils.RabbitMQUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * @program: RabbitMQ-Study
 * @author: 吴磊
 * @create: 2021-12-14 11:12
 * 消息在手动应答时候是不丢失的，如果消费失败则放回队列中重新消费
 */
public class Task2 {
    private static final String TASK_ACK_QUEUE = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMQUtils.getChannel();
        //开启发布确认
//        channel.confirmSelect();
        //声明队列
        //让消息队列持久化
        boolean durable = true;
        channel.queueDeclare(TASK_ACK_QUEUE, durable, false, false, null);
        //从控制台输入信息
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String message = scanner.next();
            //消息持久化  ： MessageProperties.PERSISTENT_TEXT_PLAIN
            channel.basicPublish("", TASK_ACK_QUEUE, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes(StandardCharsets.UTF_8));
            System.out.println("生产者发送消息：" + message);
        }
    }
}
